package com.android.dumiaogou.fragment.member;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.dumiaogou.R;
import com.android.dumiaogou.activity.BaseObject;
import com.android.dumiaogou.fragment.BaseLazyFragment;
import com.android.dumiaogou.model.member.CustomerQuestData;
import com.android.dumiaogou.model.member.CustomerQuestDataCallback;
import com.android.dumiaogou.utils.ConstantsUrl;
import com.android.dumiaogou.utils.HttpUtils;
import com.android.dumiaogou.utils.MyLog;
import com.android.dumiaogou.utils.ObjectUtils;
import com.android.dumiaogou.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 物流详情页
 */
public class Fragment_Mine_Customer extends BaseLazyFragment implements View.OnClickListener {


    @BindView(R.id.top)
    ImageView top;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.detail_rate_tip)
    RelativeLayout detailRateTip;
    private FragmentActivity mcontext;
    private View view;
    private boolean networkConnected;
    private QuestionAdapt adapter;
    private View headView;
    private TextView headTel;
    private TextView headQQ;
    private TextView headTime;
    ArrayList<MultiItemEntity> list = new ArrayList<>();
    private View footView;

    public Fragment_Mine_Customer() {

    }

    public static Fragment_Mine_Customer newInstance() {
        Fragment_Mine_Customer fragment = new Fragment_Mine_Customer();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initPrepare() {
        ReFreshData();
    }

    private void initRecycle() {
        adapter = new QuestionAdapt(list);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.addHeaderView(headView);
        adapter.addFooterView(footView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.GetToTop(top, 0);
    }

    @Override
    protected void onInvisible() {
    }


    @Override
    protected void initData() {
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        if (networkConnected) {
            getQuestion();
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_directbuy_goodsdtail_comment, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        headView = inflater.inflate(R.layout.fragment_mine_customer_head, null, false);
        footView = inflater.inflate(R.layout.fragment_mine_customer_foot, null, false);
        headTel = (TextView) headView.findViewById(R.id.head_tv_tel);
        headQQ = (TextView) headView.findViewById(R.id.head_tv_qq);
        headTime = (TextView) headView.findViewById(R.id.head_tv_worktime);
        headTel.setOnClickListener(this);
        headQQ.setOnClickListener(this);
        initRecycle();
        detailRateTip.setVisibility(View.GONE);
        return view;
    }

    private void ReFreshData() {
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                if (networkConnected) {
                    getQuestion();
                } else {
                    ObjectUtils.toast(mcontext, "网络连接出现异常");
                    mRefreshLayout.setRefreshing(false);
                }
            }
        });
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        getQuestion();
    }

    private void getQuestion() {
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBER_CUSTOMER + "?cat=1";
        MyLog.e("地址", address);

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            HttpUtils.get(address, null, dataCallback);
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            ObjectUtils.toast(getActivity(), "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }

    private String business_tel;
    private String contact_qq;
    CustomerQuestDataCallback dataCallback = new CustomerQuestDataCallback() {
        @Override
        public void onError(Call call, Exception e, int id) {
            mRefreshLayout.setRefreshing(false);
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(CustomerQuestData response, int id) {
            mRefreshLayout.setRefreshing(false);
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            if (response.getCode() == 0) {
                business_tel = response.getBusiness_tel();
                headTel.setText(business_tel);
                contact_qq = response.getContact_qq();
                headQQ.setText(contact_qq);
                headTime.setText(response.getWorktime());
                List<CustomerQuestData.CatlistBean> catlist = response.getCatlist();
                if (catlist != null) {
                    for (int i = 0; i < catlist.size(); i++) {
                        CustomerQuestData.CatlistBean catlistBean = catlist.get(i);
                        for (int k = 0; k < catlist.get(i).getList().size(); k++) {
                            catlistBean.addSubItem(catlistBean.getList().get(k));
                        }
                    }
                    list.addAll(catlist);
                    adapter.setNewData(list);
                    expandAll();
                }
            } else {
                noData.setVisibility(View.VISIBLE);
                noDataTv.setText(response.getMsg());
                noDataTv.setVisibility(View.VISIBLE);
            }
        }
    };

    private void expandAll() {
        for (int i = 0; i <list.size() ; i++) {
            adapter.expand(i + adapter.getHeaderLayoutCount(), false, false);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.head_tv_tel:
//                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+business_tel));
//                startActivity(intent);

//                Intent callIntent = new Intent(Intent.ACTION_CALL);
//                callIntent.setData(Uri.parse("tel:"+business_tel));
//                startActivity(callIntent);

                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:"+business_tel));
                startActivity(callIntent);
                break;
            case R.id.head_tv_qq:
                if (ObjectUtils.checkApkExist(mcontext, "com.tencent.mobileqq")){
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin="+contact_qq+"&version=1")));
                }else{
                    Toast.makeText(mcontext,"本机未安装QQ应用",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private class QuestionAdapt extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

        public QuestionAdapt(List<MultiItemEntity> data) {
            super(data);
            addItemType(0, R.layout.item_customer_expandable_lv0);
            addItemType(1, R.layout.item_customer_expandable_lv1);
        }

        @Override
        protected void convert(final BaseViewHolder holder, final MultiItemEntity data) {
            switch (holder.getItemViewType()) {
                case 0:
                    final CustomerQuestData.CatlistBean catlistBean = (CustomerQuestData.CatlistBean) data;
                    int position = holder.getLayoutPosition();
                    if(position == 1){
                        holder.getView(R.id.line).setVisibility(View.GONE);
                    }else {
                        holder.getView(R.id.line).setVisibility(View.VISIBLE);
                    }
                    holder.setText(R.id.lv0_title, catlistBean.getCatname());
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = holder.getAdapterPosition();
                            if (catlistBean.isExpanded()) {
                                collapse(pos);
                            } else {
                                expand(pos);
                            }
                        }
                    });
                    break;
                case 1:
                    final CustomerQuestData.CatlistBean.ListBean listBean = (CustomerQuestData.CatlistBean.ListBean) data;
                    holder.setText(R.id.lv1_title, listBean.getTitle());
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mcontext, BaseObject.class);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "常见问题");
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_PROBLEM_TITLE, listBean.getTitle());
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_PROBLEM_CONTENT, listBean.getContent());
                            mcontext.startActivity(intent);
                        }
                    });
                    break;
            }
        }
    }
}
